//
//  ViewController.m
//  GcdInsightTest
//
//  Created by 小禹宝宝 on 2017/9/7.
//  Copyright © 2017年 cy55. All rights reserved.
//

#import "ViewController.h"
#import "UIImage+HXImage.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"测试咯";
    self.view.backgroundColor = [UIColor whiteColor];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//    [self bingxingyibuqiantaozhu];
//    [self bingxingyibuqiantaoyibuzhu];
//    [self exampleFirst];
//    [self exampleSecond];
//    [self GcdGroupexampleFirst];
    
    
}

//异步并行回调同步串行主队列
- (void)bingxingyibuqiantaozhu{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"11-11  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"11  %@",[NSThread currentThread]);
            //打印结果证明了，内部顺序会遵循外部异步并行队列顺序执行（当异步并行队列全部执行完后开始一次执行内部同步串行主队列）
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"22-22  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"22  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"33-33  %@",[NSThread currentThread]);
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"33  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"44-44  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"44  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"55-55  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_main_queue(), ^{
            NSLog(@"55  %@",[NSThread currentThread]);
        });
    });
}
//异步并行内回调异步串行主队列（意义不大）
- (void)bingxingyibuqiantaoyibuzhu {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"11-11  %@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"11  %@",[NSThread currentThread]);
            //打印结果证明了，当外部异步并行队列开始执行时，内部回调有可能已经执行完了一个异步串行主队列
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"22-22  %@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"22  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"33-33  %@",[NSThread currentThread]);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"33  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"44-44  %@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"44  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"55-55  %@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"55  %@",[NSThread currentThread]);
        });
    });

}

//异步并行内回调同步串行队列
- (void)exampleFirst {
    dispatch_queue_t queue = dispatch_queue_create("com.hao123.www", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---11  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            //打印结果与主队列bingxingyibuqiantaozhu函数保持一致
            NSLog(@"11  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---22  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"22  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---33  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"33  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---44  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"44  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---55  %@",[NSThread currentThread]);
        dispatch_sync(queue, ^{
            NSLog(@"55  %@",[NSThread currentThread]);
        });
    });
}



//异步并行回调同步并行队列
- (void)exampleSecond {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---11  %@",[NSThread currentThread]);
        //对比异步并行回调同步串行，可以看出当外部异步并行队列未执行完毕时，有可能内部同步并行队列已经开始执行了
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"11  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---22  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"22  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---33  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"33  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---44  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"44  %@",[NSThread currentThread]);
        });
    });
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSLog(@"外部---55  %@",[NSThread currentThread]);
        dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSLog(@"55  %@",[NSThread currentThread]);
        });
    });
}

//论坛链接http://www.cnblogs.com/ziyi--caolu/p/4900650.html


- (void)GcdGroupexampleFirst {
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_queue_create("com.gcd-jianshu.www", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"group中的第一个--- %@",[NSThread currentThread]);
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"group中的第二个--- %@",[NSThread currentThread]);
    });
    
    dispatch_group_async(group, queue, ^{
        NSLog(@"group中的第三个--- %@",[NSThread currentThread]);
    });
    
    dispatch_group_notify(group, queue, ^{
        NSLog(@"done");
    });
}
@end
